﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace WordConversionManager
{
    public enum OverwriteMethod
    { 
        Overwrite,
        SaveAppendUnique,
        Discharge
    }

    static class WordConversionManagerSetttings
    {
        public const string ServiceApplicationName = "ServiceApplicationName_ac324808-425b-49f5-9336-e8322893cc7b";
        public const string SequenceNumber = "SequenceNumber_ac324808-425b-49f5-9336-e8322893cc7b";
        public const string SupportedInputFileFormats = "SupportedFileFormats_ac324808-425b-49f5-9336-e8322893cc7b";
        public const string OutputFileFormat = "OutputFileFormat_ac324808-425b-49f5-9336-e8322893cc7b";
        public const string Overwrite = "Overwrire_ac324808-425b-49f5-9336-e8322893cc7b";
        public const string DestinationList = "DestionationList_ac324808-425b-49f5-9336-e8322893cc7b";

        public static string GetSupportedFileFormats(SPList List)
        {
            return GetPropertyValue(List, SupportedInputFileFormats, "docx, docm, dotx, dotm, doc, dot, rtf, htm, html, mht, mhtml, xml");
        }

        public static string[] GetOutputFileFormats(SPList List)
        {
            return GetPropertyValue(List, OutputFileFormat, "PDF").Split(';');
        }

        public static OverwriteMethod GetOverwriteSetting(SPList List)
        {
            string overwrite = GetPropertyValue(List, Overwrite, "Overwrite");
            OverwriteMethod overwriteMethodEnum;


            if (overwrite == "SaveAppendUnique")
            {
                overwriteMethodEnum = OverwriteMethod.SaveAppendUnique;
            }
            else if (overwrite == "Discharge")
            {
                overwriteMethodEnum = OverwriteMethod.Discharge;
            }
            else
            {
                overwriteMethodEnum = OverwriteMethod.Overwrite;
            }

            return overwriteMethodEnum;
        }

        public static string GetDestinationList(SPList List)
        {
            return GetPropertyValue(List, DestinationList, "");
        }

        public static string GetWordServiceApplicationProxy(SPWeb Web)
        {
            return GetPropertyValue(Web, ServiceApplicationName, "Word Automation Services");
        }

        public static string GetSequenceNumber(SPList List)
        {
            return GetPropertyValue(List, SequenceNumber, "10000");
        }

        private static string GetPropertyValue(SPWeb Web, string PropertyName, string DefaultValue)
        {
            if (Web.Properties.ContainsKey(PropertyName))
            {
                return Web.Properties[PropertyName].ToString();
            }
            else
            {
                Web.Properties.Add(PropertyName, DefaultValue);
                Web.Properties.Update();
                return DefaultValue;
            }
        }

        private static string GetPropertyValue(SPList List, string PropertyName, string DefaultValue)
        {
            if (List.RootFolder.Properties.ContainsKey(PropertyName))
            {
                return List.RootFolder.Properties[PropertyName].ToString();
            }
            else
            {
                List.RootFolder.Properties.Add(PropertyName, DefaultValue);
                List.RootFolder.Update();
                return DefaultValue;
            }
        }

        public static void SetPropertyValue(SPWeb Web, string PropertyValue, string Value)
        {
            if (Web.Properties.ContainsKey(PropertyValue))
            {
                Web.Properties[PropertyValue] = Value;
                Web.Properties.Update();
            }
            else
            {
                Web.Properties.Add(PropertyValue, Value);
                Web.Properties.Update();
            }
        }

        public static void SetPropertyValue(SPList List, string PropertyValue, string Value)
        {
            if (List.RootFolder.Properties.ContainsKey(PropertyValue))
            {
                List.RootFolder.Properties[PropertyValue] = Value;
                List.RootFolder.Update();
            }
            else
            {
                List.RootFolder.Properties.Add(PropertyValue, Value);
                List.RootFolder.Update();
            }
        }
    }
}
